import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Queue;
import java.util.StringTokenizer;

/**
 *
 * @author Alexander Fedulin.
 */
public class lucky_genfunc
{
    static final String problem_input = "lucky.in";
    static final String problem_output = "lucky_gen_func.out";
    private StringTokenizer st;
    private BufferedReader br;
    private PrintWriter out;

    int A, B, X, Y, Z, P, C;

    void load() throws IOException
    {
    	A = nextInt();
    	B = nextInt();
    	X = nextInt();
    	Y = nextInt();
    	Z = nextInt();
    	P = nextInt();
    	C = nextInt();
    }
    long xx, yy;

    long extGcd(long a, long b)
    {
    	if (b == 0)
    	{
    		xx = 1;
    		yy = 0;
    		return a;
    	}
    	else
    	{
    		long d = extGcd(b, a % b);
    		long _x = yy;
    		long _y = xx - (a / b) * yy;
    		xx = _x;
    		yy = _y;
    		return d;
    	}
    }
	int invMod(int X)
	{
		X %= P;
		X += P;
		X %= P;
        if (X == 0)
        {
            System.err.println("Can't found inverse modulo P");
        }
		extGcd(X, P);
		xx %= P;
		xx += P;
		xx %= P;
		return (int)xx;
	}

	void solve_equation(int a, int c)
	{
		/*
		* a x = c (mod p)
		*/
		xx = (invMod(a) * c) % P;
	}
	ArrayList<Long> first, period;

    long round_up(long a, long b) throws Exception {
        long t = a / b;
        if (b * t < a) t++;
        if (b * t < a) throw new Exception("42");
        return t;
    }


void solve_diophant(int a, int b, int power1, int power2) throws Exception {
        long c = power2 - power1;
		long d = extGcd(a, -b);
		if (d < 0)
		{
			d *= -1;
			xx *= -1;
			yy *= -1;
		}
		if (c % d != 0)
			return;
		xx *= c / d;
		yy *= c / d;
		if (xx > 0)
		{
			long t = (xx * d / b);
			xx -= b / d * t;
			yy -= a / d * t;
		}
		if (xx < 0)
		{
			long t = round_up((-xx * d), b);
			xx += b / d * t;
			yy += a / d * t;
		}
		if (yy < 0)
		{
			long t = round_up((-yy * d), a);
			xx += b / d * t;
			yy += a / d * t;
		}
		first.add(power1 + xx * a);
		period.add(a * (b / d));
	}

	int was[][] = new int[2][10007];
    int a[][] = new int[3][3];
    int d[][] = new int[3][3];
    int t[][] = new int[3][3];

    int get_fib(int n)
    {
        for (int i = 0; i < 3; i++)
        {
            Arrays.fill(a[i], 0);
            Arrays.fill(d[i], 0);
        }
        for (int j = 0; j < 3; j++)
            d[j][j] = 1;
        a[0][0] = 0;
        a[1][0] = 1;
        a[2][0] = 0;
        a[0][1] = Y;
        a[1][1] = X;
        a[2][1] = Z;
        a[0][2] = 0;
        a[1][2] = 0;
        a[2][2] = 1;
        while (n > 0)
        {
            if ((n & 1) > 0)
            {
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < 3; j++)
                    {
                        t[i][j] = 0;
                        for (int k = 0; k < 3; k++)
                        {
                            t[i][j] += (d[i][k] * a[k][j]) % P;
                            if (t[i][j] >= P) t[i][j] -= P;
                        }
                    }
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < 3; j++)
                        d[i][j] = t[i][j];
            }
            for (int i = 0; i < 3; i++)
                    for (int j = 0; j < 3; j++)
                    {
                        t[i][j] = 0;
                        for (int k = 0; k < 3; k++)
                        {
                            t[i][j] += (a[i][k] * a[k][j]) % P;
                            if (t[i][j] >= P) t[i][j] -= P;
                        }
                    }
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < 3; j++)
                        a[i][j] = t[i][j];
            n >>= 1;
        }
        return (A * d[0][0] + B * d[1][0] + d[2][0]) % P;
    }
    void solve_dull() throws IOException {
         int w[][] = new int[P][P];
         for (int i = 0; i < P; i++)
            Arrays.fill(w[i], -1);
         w[A][B] = 0;
         int _1 = A;
         int _2 = B;
         int _3;
         int pred_period = 0;
         int period = 0;
         int good = 0;
         for (int i = 1; ; i++)
         {
            _3 = (_2 * X + _1 * Y + Z) % P;
            if (w[_2][_3] >= 0)
            {
                pred_period = w[_2][_3];
                period = i - w[_2][_3];
                break;
            }
            _1 = _2;
            _2 = _3;
         }
         for (int i = pred_period; i < pred_period + period; i++)
         {
            if (get_fib(i) == C) good++;
         }

         int Q = nextInt();
         long l, r;
         for (int i = 0; i < Q; i++)
         {
            l = nextLong();
            r = nextLong();
            l--;
            r--;
            long res = 0;
            while (l < pred_period && l <= r)
            {
                if (get_fib((int)l) == C) res++;
                l++;
            }
            if (l >= pred_period && r >= pred_period)
            {
                while (l <= r && ((l - pred_period) % period) != 0)
                {
                    if (get_fib((int)(pred_period + ((l - pred_period) % period))) == C) res++;
                     l++;
                }
                while (l <= r && ((r - pred_period) % period) != (period - 1))
                {
                   if (get_fib((int)(pred_period + ((r - pred_period) % period))) == C) res++;
                    r--;
                }
                if (l <= r)
                {
                    l -= pred_period;
                    r -= pred_period;
                    l /= period;
                    r /= period;
                    if (l <= r)
                     res += (r - l + 1) * good;
                }
            }
            out.println(res);
         }
    }
    void prepareWithoutY()
    {
        if (X == 1)
        {
            int add_first = Z;
            int t1 = (A - Z) % P;
            t1 += P;
            t1 %= P;
            int t2 = (B - A * X) % P - A;
            t2 += Z * 2;
            t2 %= P;
            t2 += P;
            t2 %= P;
        }
    }
    int findl(ArrayList <Integer> a, int key)
    {
        int l = 0;
        int r = a.size() - 1;
        int mid;
        int res = a.size();
        while (l <= r)
        {
            mid = (l + r) >> 1;
            if (a.get(mid) >= key)
            {
                res = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        return res;
    }
    int findr(ArrayList <Integer> a, int key)
    {
        int l = 0;
        int r = a.size() - 1;
        int mid;
        int res = -1;
        while (l <= r)
        {
            mid = (l + r) >> 1;
            if (a.get(mid) <= key)
            {
                res = mid;
                l = mid + 1;
            }
            else r = mid - 1;
        }
        return res;
    }
    void solve_matrix() throws Exception {
        ArrayList <Integer> all = new ArrayList<Integer>();
        ArrayList <Integer> pred = new ArrayList<Integer>();
        ArrayList <Integer> cycle = new ArrayList<Integer>();
        int big1 = get_fib(P * P + 1);
        int big2 = get_fib(P * P + 2);
        int period = -1;
        for (int k = 1; k <= P; k++)
        {
            if ((P * P - 1) % k == 0)
            {
                int big3 = get_fib(P * P + 1 + k);
                int big4 = get_fib(P * P + 2 + k);

                if (big3 == big1 && big4 == big2)
                {
                    period = k;
                    break;
                }

                if ((P * P - 1) / k != k)
                {
                    big3 = get_fib(P * P + 1 + (P * P - 1) / k);
                    big4 = get_fib(P * P + 2 + (P * P - 1) / k);
                    if (big1 == big3 && big2 == big4)
                        period = (P * P - 1) / k;
                }
            }
        }
        if (period == -1)
        {
             for (int k = 1; k <= P; k++)
        {
            if ((P * (P - 1)) % k == 0)
            {
                int big3 = get_fib(P * P + 1 + k);
                int big4 = get_fib(P * P + 2 + k);

                if (big3 == big1 && big4 == big2)
                {
                    period = k;
                    break;
                }

                if ((P * (P - 1)) / k != k)
                {
                    big3 = get_fib(P * P + 1 + (P * (P - 1)) / k);
                    big4 = get_fib(P * P + 2 + (P * (P - 1)) / k);
                    if (big1 == big3 && big2 == big4)
                        period = (P * (P - 1)) / k;
                }
            }
        }
            if (period == -1)
            {
                throw new Exception("Can't calculate period for " + P + " " + (P * P - 1));
            }
        }
        big1 = A;
        big2 = B;
        int big3 = get_fib(period);
        int big4 = get_fib(period + 1);
        int pred_period;
        int _3;
        for (pred_period = 0; A != big1 || B != big2; pred_period++)
        {
            if (big1 == C)
                pred.add(pred_period);
            _3 = (big2 * X + big1 * Y + Z) % P;
            big1 = big2;
            big2 = _3;
            _3 = (big4 * X + big3 * Y + Z) % P;
            big3 = big4;
            big4 = _3;
        }
        for (int i = 0; i < period; i++)
        {
            if (big1 == C)
                cycle.add(i);
            _3 = (big2 * X + big1 * Y + Z) % P;
            big1 = big2;
            big2 = _3;
        }
       // System.err.println(pred_period);
     //   System.err.println(pred.size() + " " + cycle.size());
        int Q = nextInt();
        long res = 0;
        long p1, p2;
        for (int i = 0; i < Q; i++)
        {
            long l = nextLong();
            long r = nextLong();
            l--;
            r--;
            res = 0;
            if (l < pred_period && r < pred_period)
            {
                int q1 = findl(pred, (int)l);
                int q2 = findr(pred, (int)r);
                if (q1 <= q2)
                    res += q2 - q1 + 1;
                out.println(res);
                continue;
            }
            if (l < pred_period)
            {
                int q1 = findl(pred, (int)l);
                res += pred.size() - q1;
                l = pred_period;
            }
            l -= pred_period;
            r -= pred_period;
            p1 = l / period;
            p2 = r / period;
            if (p1 == p2)
            {
                int q1 = findl(cycle, (int)(l % period));
                int q2 = findr(cycle, (int)(r % period));
                if (q1 <= q2)
                    res += q2 - q1 + 1;
            }
            else
            {
                int q1 = findl(cycle, (int)(l % period));
                int q2 = findr(cycle, (int)(r % period));
                res += cycle.size() - q1;
                res += q2 + 1;
                p1++;
                p2--;
                if (p1 <= p2)
                    res += (p2 - p1 + 1) * (long)cycle.size();
            }
            out.println(res);
        }

    }
    void solve() throws Exception
    {
        if (P == 2 || X == 0 || Y == 0)
        {
            solve_matrix();
            return;
        }

    	int t1 = A;
    	int t2 = (B - A * X) % P;
    	int t3 = Z;
    	int D = (((X * X) % P) + 4 * Y) % P;
    	if (D == 0)
        {
            solve_matrix();
            return;
        }
      //  System.err.println(D);
    	int sqD;
    	for (sqD = 0; sqD < P; sqD++)
    		if ((sqD * sqD) % P == D)
    			break;
    //    System.err.println(sqD);
    	if (sqD == P)
        {
            solve_matrix();
            return;
           // throw new Exception("42");
        }
    	int p1 = -((2 * Y) % P) * invMod(X + sqD);
    	int p2 = -((2 * Y) % P) * invMod(X - sqD);

    	p1 %= P;
    	p2 %= P;
    	p1 += P;
    	p1 %= P;
    	p2 += P;
    	p2 %= P;
        if (p1 == 1 || p2 == 1 || p2 == 0 || p1 == 0)
        {
            solve_matrix();
            return;
        }
    	int alpha = t3 * invMod(1 - p1);
    	alpha %= P;
    	alpha *= invMod(1 - p2);
    	alpha %= P;
    	alpha += P;
    	alpha %= P;
    	int beta = (t1 + ((t2 * invMod(p1)) % P));
    	beta %= P;
    	beta *= (1 - invMod(p1)) % P;
    	beta %= P;
    	beta += (((t3 * invMod(p1)) % P) * invMod(p1)) % P;
    	beta %= P;
    	beta *= invMod(1 - p2 * invMod(p1));
    	beta %= P;
    	beta += P;
    	beta %= P;
    	beta *= invMod(1 - invMod(p1));
    	beta %= P;
    	beta += P;
    	beta %= P;
    	int gamma = (t1 + ((t2 * invMod(p2)) % P));
    	gamma %= P;
    	gamma *= (1 - invMod(p2)) % P;
    	gamma %= P;
    	gamma += (((t3 * invMod(p2)) % P) * invMod(p2)) % P;
    	gamma %= P;
    	gamma *= invMod(1 - p1 * invMod(p2));
    	gamma %= P;
    	gamma += P;
    	gamma %= P;
    	gamma *= invMod(1 - invMod(p2));
    	gamma %= P;
    	gamma += P;
    	gamma %= P;

    	int pp1 = 1;
    	int period1 = 0;
    	Arrays.fill(was[0], -1);
    	for (int i = 0; ; i++)
    	{
    		if (was[0][pp1] >= 0)
    		{
    			period1 = i - was[0][pp1];
    			break;
    		}
    		was[0][pp1] = i;
    		pp1 *= p1;
    		pp1 %= P;
    	}
    	int pp2 = 1;
    	int period2 = 0;
    	Arrays.fill(was[1], -1);
    	for (int i = 0; ; i++)
    	{
    		if (was[1][pp2] >= 0)
    		{
    			period2 = i - was[1][pp2];
    			break;
    		}
    		was[1][pp2] = i;
    		pp2 *= p2;
    		pp2 %= P;
    	}


    	first = new ArrayList <Long>();
    	period = new ArrayList <Long> ();
    	if (beta == 0 && gamma == 0)
    	{
    		if (alpha == C)
    		{
    			first.add((long)0);
    			period.add((long)1);
    		}
    	}
    	else
    	{
    		if (beta > gamma)
    		{
    			int t = beta;
    			beta = gamma;
    			gamma = t;
    			t = p1;
    			p1 = p2;
    			p2 = t;
                for (int i = 0; i < P; i++)
                {
                    t = was[0][i];
                    was[0][i] = was[1][i];
                    was[1][i] = t;
                }
    		}
    		for (int i = 0; i < P; i++)
    		{
    			solve_equation(gamma, ((((C - alpha - beta * i) % P) + P) % P));
    			int power1 = was[0][i];
    			int power2 = was[1][(int)xx];
    			if (power1 >= 0 && power2 >= 0)
    				solve_diophant(period1, period2, power1, power2);
    		}
    	}
    	long l, r;
        int Q = nextInt();
    	for (int i = 0; i < Q; i++)
    	{
    		l = nextLong();
    		r = nextLong();
            l--;
            r--;
    		long res = 0;
    		for (int j = 0; j < first.size(); j++)
    		{
                if (r >= first.get(j))
                {
    			    long q1 = round_up(l - first.get(j), (long)period.get(j));
    			    long q2 = (r - first.get(j)) / (long)period.get(j);
    			    if (q1 <= q2)
    			    	res += q2 - q1 + 1;
                }
    		}
    		out.println(res);
    	}
    	/*int pp1 = 1, pp2 = 1;
    	for (int i = 0; i < 10000; i++)
    	{
    		out.print(((alpha + beta * pp1 + gamma * pp2) % P) + " ");
    		pp1 *= p1;
    		pp2 *= p2;
    		pp1 %= P;
    		pp2 %= P;
       	}
       	*/

    }

    public void run()
    {
        try
        {
            boolean oj = System.getProperty("ONLINE_JUDGE") != null;
            Reader reader = oj ? new InputStreamReader(System.in) : new FileReader(problem_input);
            Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter(problem_output);
            br = new BufferedReader(reader);
            out = new PrintWriter(writer);
            int T = nextInt();
            for (int i = 0; i < T; i++)
            {
            	load();
            	solve();
            }
            out.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.exit(42);
        }
    }

    private String nextToken() throws IOException
    {
        while (st == null || (!st.hasMoreTokens()))
        {
                st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException
    {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException
    {
        return Long.parseLong(nextToken());
    }

    void nextArray(int a[], int size) throws IOException
    {
        a = new int[size];
        for (int i = 0; i < size; i++)
                a[i] = nextInt();
    }

    void nextArray(double a[], int size) throws IOException
    {
        a = new double[size];
        for (int i = 0; i < size; i++)
            a[i] = nextDouble();
    }

    void nextArray(String a[], int size) throws IOException
    {
        a = new String[size];
        for (int i = 0; i < size; i++)
            a[i] = nextToken();
    }

    String readAll() throws IOException
    {
        String a = "";
		st = null;
		while (br.ready())
			a += br.read();
        return a;
    }

    double nextDouble() throws IOException
    {
		return Double.parseDouble(nextToken());
    }

    public static void main(String []args)
    {
		new lucky_genfunc().run();
    }
}
